package settingsapp.ui.panel;

import settingsapp.settings.continuous.ContinuousSettingFactory;
import settingsapp.settings.toggle.ToggleSettingFactory;
import settingsapp.settings.toggle.ToggleSettingFactory.ToggleSettingType;
import settingsapp.ui.PanelGridActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.AttributeSet;
import ece.settingsapp.R;

public class TogglePanel extends Panel
{
	protected boolean selected = true;
	
	protected int iconResourceOn, iconResourceOff;
	
	private static final String TAG = TogglePanel.class.getSimpleName();

	/**
	 * Called after Button's default constructors to
	 * initialize all custom aspects
	 */
	protected void init()
	{
		setText(name);
		setBackgroundResource(R.drawable.button_selector);
		
		// add the foreground image from iconResourceId
		setForegroundImage();
		
		setSettingType(ToggleSettingType.unset);
	}
	
	public void setSettingType(ToggleSettingType type)
	{
		// create object to modify settings
		changer = ToggleSettingFactory.createSettingsObject(context, type);
		setIconResourcesFromSettingObject();	// get the image resources appropriate for the new setting
		selected = changer.getValue();			// store the current value of the system setting
		setForegroundImage();					// and change the image based on that information
	}
	
	public void setSettingType(int mainSettingType, int typeIndex)
	{
		// create object to modify settings
		switch (mainSettingType)
		{
		case 0:
			changer = ToggleSettingFactory.createSettingsObject(context, typeIndex);
			break;
		case 1:
			changer = ContinuousSettingFactory.createSettingsObject(context, typeIndex);
		}
		
		setIconResourcesFromSettingObject();	// get the image resources appropriate for the new setting
		selected = changer.getValue();			// store the current value of the system setting
		setForegroundImage();					// and change the image based on that information
	}
	
	private void setIconResourcesFromSettingObject()
	{
		iconResourceOn = changer.getResourceOn();
		iconResourceOff = changer.getResourceOff();
	}
	
	/**
	 * Set the foreground image based on whether this toggle setting is selected or not
	 */
	protected void setForegroundImage()
	{
		setCompoundDrawablesWithIntrinsicBounds(android.R.color.transparent, 
				android.R.color.transparent, selected ? iconResourceOn : iconResourceOff, 
						android.R.color.transparent);
	}
	
	@Override
	public Runnable getClickAction()
	{
		return new Runnable()
		{
			public void run()
			{
				selected = changer.clickAction();		// toggle setting and store the new value
				setForegroundImage();				// change image based on new value
			}
		};
	}
	
	
	public TogglePanel(PanelGridActivity context)
	{
		super(context);
	}

	public TogglePanel(PanelGridActivity context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	public TogglePanel(PanelGridActivity context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}

	@Override
	public void showSetTypeDialog()
	{
		final CharSequence[] items = {"Toggle Setting", "Continuous Setting"};
		
		// display a dialog to select a new setting type for this Panel
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Choose a setting type");
		builder.setItems(items, new DialogInterface.OnClickListener()
		{
		    public void onClick(DialogInterface dialog, int item)
		    {
		    	switch (item)
		    	{
		    	case 0:
		    		showSetTypeSubDialogToggle();
		    		break;
		    	case 1:
		    		showSetTypeSubDialogContinuous();
		    		break;
		    	}
		    }
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	private void showSetTypeSubDialogToggle()
	{
		final CharSequence[] items = ToggleSettingFactory.getSettingTypes();		// retrieve the names of the different possibilities
		
		// display a dialog to select a new setting type for this Panel
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Choose a setting");
		builder.setItems(items, new DialogInterface.OnClickListener()
		{
		    public void onClick(DialogInterface dialog, int item)
		    {
		    	setSettingType(0, item);
		    }
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	private void showSetTypeSubDialogContinuous()
	{
		final CharSequence[] items = ContinuousSettingFactory.getSettingTypes();		// retrieve the names of the different possibilities
		
		// display a dialog to select a new setting type for this Panel
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Choose a setting");
		builder.setItems(items, new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int item)
			{
				setSettingType(1, item);
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
